package com.hanlink.dsp.ui.activities;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.SPUtils;
import com.edge.pbxdsp.R;
import com.hanlink.dsp.DspContext;

import com.hanlink.dsp.ui.CaAmpDspActivity;
import com.qztech.utils.YzDebug;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
//
//import butterknife.ButterKnife;
//import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class HomeActivity extends AppCompatActivity {
    private Disposable mClickDisposable;
    private Disposable mDisposable;


    private int mEmailClickCount = 0;
    private Handler mHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_home);
       // ButterKnife.bind(this);
//        requestPermissions();

        mHandler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                mEmailClickCount = 0;
            }
        };
        mDisposable = Flowable.timer(500, TimeUnit.MILLISECONDS)
                .subscribe(aLong -> goMain(), Throwable::printStackTrace);
        findViewById(R.id.email).setOnClickListener(v->{
            mEmailClickCount++;
            mHandler.removeMessages(0);
            if (mEmailClickCount >= 6){
                //reset password
                SPUtils.getInstance(Context.MODE_PRIVATE)
                        .put(DspContext.KEY_PWD, DspContext.PASSOWRD);
                setPasswordResetDialog(this);
                YzDebug.d("onCreate  mEmailClickCount:"+ mEmailClickCount);
                mEmailClickCount = 0;
            }else{
                mHandler.sendEmptyMessageDelayed(0, 1000);
            }

        });

        test();


        findViewById(R.id.rootView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goMain();
            }
        });
    }

    private void test(){
        DisplayMetrics d = getResources().getDisplayMetrics();
        YzDebug.d("test w:"+ d.widthPixels +" h:"+ d.heightPixels +" density:"
        + d.densityDpi +" dpi:"+ d.density +" wp:"+ (d.widthPixels/d.density)
        +" hp:"+(d.heightPixels/d.density));
    }

    @Override
    protected void onResume() {
        super.onResume();
        mEmailClickCount = 0;
    }

//    @OnClick({R.id.rootView})
//    public void onClick(View v) {
//        debounceClick(v.getId(), id -> {
//            switch (id) {
//                case R.id.rootView: {
//                    goMain();
//                    break;
//                }
//            }
//        });
//    }

    private void goMain() {
        Intent intent = new Intent(HomeActivity.this, CaAmpDspActivity.class);
        startActivity(intent);
    }

    @SuppressLint("CheckResult")
    private void debounceClick(int viewId, Consumer<Integer> consumer) {
        if (mClickDisposable != null && !mClickDisposable.isDisposed()) {
            mClickDisposable.dispose();
        }
        mClickDisposable = Flowable.just(viewId)
                .subscribeOn(Schedulers.computation())
                .debounce(1000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(consumer, Throwable::printStackTrace);
    }

    private void requestPermissions() {
        List<String> permissions = new ArrayList<>();
        String[] permissionArray = {
                Manifest.permission.ACCESS_FINE_LOCATION
        };
        for (String p : permissionArray) {
            if (ContextCompat.checkSelfPermission(this,
                    p)
                    != PackageManager.PERMISSION_GRANTED) {
                permissions.add(p);
            }
        }
        if (permissions.size() > 0) {
            ActivityCompat.requestPermissions(this,
                    permissions.toArray(new String[permissions.size()]),
                    0);
        }
    }

    @Override
    protected void onStop() {
        if (mClickDisposable != null && !mClickDisposable.isDisposed()) {
            mClickDisposable.dispose();
        }

        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.isDisposed();
        }
        super.onStop();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if (BluetoothClientManager.getInstance(BtDspApplication.getInstance()).isConnect()) {
//            BluetoothClientManager.getInstance(BtDspApplication.getInstance()).disconnect();
//        }
    }



    private void setPasswordResetDialog(Context ctx) {
        androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(ctx, com.qztech.sdk.R.style.dialog);
        final View view = LayoutInflater.from(ctx).inflate(R.layout.dialog_password_reset_success, null, false);
        androidx.appcompat.app.AlertDialog alertDialog = builder.setView(view)
//                .setTitle(R.string.title_change_password)
                .setCancelable(false)
                .create();
        view.findViewById(R.id.ok).setOnClickListener(v -> {
            alertDialog.dismiss();
        });
        alertDialog.show();

    }
}
